package schooltg.core.persist.dao.impl;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.query.dsl.QueryBuilder;
import schooltg.core.data.User;
import schooltg.core.persist.dao.UserDao;
import schooltg.core.persist.entity.PostPO;
import schooltg.core.persist.entity.UserPO;
import mtons.modules.annotation.Repository;
import mtons.modules.persist.impl.BaseRepositoryImpl;
import mtons.modules.pojos.Paging;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;
import java.util.Set;


@Repository(entity = UserPO.class)
public class UserDaoImpl extends BaseRepositoryImpl<UserPO> implements UserDao {
	private static final long serialVersionUID = -3396151113305189145L;

	@Override
	public UserPO getByUsername(String username) {
		return findUniqueBy("username", username);
	}

	@Override
	public UserPO getByEmail(String email) {
		return findFirst("email", email);
	}

	@Override
	public List<UserPO> paging(Paging paging, String key) {
		PagingQuery<UserPO> q = pagingQuery(paging);
		if (StringUtils.isNotBlank(key)) {
			q.add(Restrictions.or(
				Restrictions.like("username", key, MatchMode.ANYWHERE),
				Restrictions.like("name", key, MatchMode.ANYWHERE)
			));
		}
		q.desc("id");
		return q.list();
	}
	
	@Override
	public List<UserPO> getHotUserByfans(int maxResults) {
		Criteria c = createCriteria();
		c.setMaxResults(maxResults);
		c.addOrder(Order.desc("fans"));
		return c.list();
		
	}

	@Override
	public List<UserPO> findByIds(Set<Long> ids) {
		return find(Restrictions.in("id", ids));
	}

	@Override
	public void identityPost(List<Long> userIds, boolean identity) {
		String substm = "+ 1";

		if (!identity) {
			substm = "- 1";
		}
		Query query = createSQLQuery("update stg_users set posts = posts " + substm + " where id in (:ids)");
		query.setParameterList("ids", userIds);
		query.executeUpdate();
	}

	@Override
	public void identityComment(List<Long> userIds, boolean identity) {
		String substm = "+ 1";

		if (!identity) {
			substm = "- 1";
		}
		Query query = createSQLQuery("update stg_users set comments = comments " + substm + " where id in (:ids)");
		query.setParameterList("ids", userIds);
		query.executeUpdate();
	}

	@Override
	public void identityFollow(List<Long> userIds, boolean identity) {
		String substm = "+ 1";

		if (!identity) {
			substm = "- 1";
		}
		Query query = createSQLQuery("update stg_users set follows = follows " + substm + " where id in (:ids)");
		query.setParameterList("ids", userIds);
		query.executeUpdate();
	}

	@Override
	public void identityFans(List<Long> userIds, boolean identity) {
		String substm = "+ 1";

		if (!identity) {
			substm = "- 1";
		}
		Query query = createSQLQuery("update stg_users set fans = fans " + substm + " where id in (:ids)");
		query.setParameterList("ids", userIds);
		query.executeUpdate();
	}

	@Override
	public void identityFavors(List<Long> userIds, boolean identity) {
		String substm = "+ 1";

		if (!identity) {
			substm = "- 1";
		}
		Query query = createSQLQuery("update stg_users set favors = favors " + substm + " where id in (:ids)");
		query.setParameterList("ids", userIds);
		query.executeUpdate();
	}

	@Override
	public List<User> seach(Paging page, String q) {
//		Query query = createSQLQuery("select * from stg_users where name like (:ids) limit");
//		query.setString("ids","%" + q + "%");
//		query.setFirstResult(page.getFirstResult());
//		query.setMaxResults(page.getMaxResults());

		FullTextSession fullTextSession = Search.getFullTextSession(super.session());
		SearchFactory sf = fullTextSession.getSearchFactory();
		QueryBuilder qb = sf.buildQueryBuilder().forEntity(UserPO.class).get();

		org.apache.lucene.search.Query luceneQuery  = qb.keyword().onFields("name","username").matching(q).createQuery();

		FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery);
		query.setFirstResult(page.getFirstResult());
		query.setMaxResults(page.getMaxResults());

		StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
		SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span style='color:red;'>", "</span>");
		QueryScorer queryScorer = new QueryScorer(luceneQuery);
		Highlighter highlighter = new Highlighter(formatter, queryScorer);
		List<User> userList = query.list();
		return userList;
	}


}
